package com.ruoyi.web.controller.djb;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.djb.domain.DjbAdmin;
import com.ruoyi.djb.domain.DjbBuilding;
import com.ruoyi.djb.domain.DjbDistrict;
import com.ruoyi.djb.domain.DjbGate;
import com.ruoyi.djb.mapper.DjbAdminMapper;
import com.ruoyi.djb.mapper.DjbBuildingMapper;
import com.ruoyi.djb.mapper.DjbGateMapper;
import com.ruoyi.djb.service.IDjbDistrictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.djb.config.Constants.ADMIN_TYPE_ADMIN;

/**
 * 小区管理Controller
 * 
 * @author xxd
 * @date 2023-12-08
 */
@Controller
@RequestMapping("/djb/district")
public class DjbDistrictController extends BaseController
{
    private String prefix = "djb/district";

    @Autowired
    private IDjbDistrictService djbDistrictService;
    @Resource
    private DjbAdminMapper adminMapper;
    @Resource
    private DjbGateMapper gateMapper;
    @Resource
    private DjbBuildingMapper buildingMapper;

    @RequiresPermissions("djb:district:view")
    @GetMapping()
    public String district()
    {
        return prefix + "/district";
    }

    /**
     * 查询小区管理列表
     */
    @RequiresPermissions("djb:district:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjbDistrict djbDistrict)
    {
        startPage();
        List<DjbDistrict> list = djbDistrictService.selectDjbDistrictList(djbDistrict);
        return getDataTable(list);
    }

    /**
     * 导出小区管理列表
     */
    @RequiresPermissions("djb:district:export")
    @Log(title = "小区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DjbDistrict djbDistrict)
    {
        List<DjbDistrict> list = djbDistrictService.selectDjbDistrictList(djbDistrict);
        ExcelUtil<DjbDistrict> util = new ExcelUtil<DjbDistrict>(DjbDistrict.class);
        return util.exportExcel(list, "小区管理数据");
    }

    /**
     * 新增小区管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存小区管理
     */
    @RequiresPermissions("djb:district:add")
    @Log(title = "小区管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DjbDistrict djbDistrict)
    {
        return toAjax(djbDistrictService.insertDjbDistrict(djbDistrict));
    }

    /**
     * 修改小区管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        DjbDistrict djbDistrict = djbDistrictService.selectDjbDistrictById(id);
        mmap.put("djbDistrict", djbDistrict);

        DjbAdmin check = new DjbAdmin();
        check.setDistrictId(djbDistrict.getId());
        check.setType(ADMIN_TYPE_ADMIN);
        List<DjbAdmin> djbAdmins = adminMapper.selectDjbAdminList(check);
        if(!djbAdmins.isEmpty()){
            check=djbAdmins.get(0);
            djbDistrict.setAdminName(check.getName());
            djbDistrict.setAdminPhone(check.getMobile());
        }
        DjbAdmin check2 = new DjbAdmin();
        check2.setDistrictId(djbDistrict.getId());
        List<DjbAdmin> djbAdmins2 = adminMapper.selectDjbAdminList(check2);
        djbDistrict.setYuangongNum(djbAdmins2.size());

        DjbBuilding building = new DjbBuilding();
        building.setDistrictId(djbDistrict.getId());
        List<DjbBuilding> djbBuildings = buildingMapper.selectDjbBuildingList(building);
        djbDistrict.setBuildNum(djbBuildings.size());

        DjbGate gate = new DjbGate();
        gate.setDistrictId(djbDistrict.getId());
        List<DjbGate> djbGates = gateMapper.selectDjbGateList(gate);
        djbDistrict.setGateNum(djbGates.size());


        return prefix + "/edit";
    }

    /**
     * 修改保存小区管理
     */
    @RequiresPermissions("djb:district:edit")
    @Log(title = "小区管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DjbDistrict djbDistrict)
    {
        return toAjax(djbDistrictService.updateDjbDistrict(djbDistrict));
    }

    /**
     * 删除小区管理
     */
    @RequiresPermissions("djb:district:remove")
    @Log(title = "小区管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(djbDistrictService.deleteDjbDistrictByIds(ids));
    }
}
